{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import joblib\n",
    "from sklearn.model_selection import KFold\n",
    "from sklearn.model_selection import train_test_split\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# train data\n",
    "train_data = joblib.load('./x_train.lz4')\n",
    "data_label = pd.read_csv('../../preprocess_data/train_y_33465.csv',usecols=['label'])\n",
    "x_train = train_data.fillna(-1).values\n",
    "y_train = data_label.values.ravel()\n",
    "# valid data\n",
    "valid_data = joblib.load('./x_valid.lz4')\n",
    "x_valid = valid_data.fillna(-1).values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# train\n",
    "import lightgbm as lgb\n",
    "parameters = {\n",
    "    'boost':'gbdt',\n",
    "    'num_leaves':150, \n",
    "    'scale_pos_weight':float(len(y_train)-np.sum(y_train))/float(np.sum(y_train)),\n",
    "    'max_depth':-1,\n",
    "    'learning_rate':.05,\n",
    "    'max_bin':250,\n",
    "    'min_data_in_leaf' : 60,\n",
    "    'objective':'binary',\n",
    "    'metric':'auc',\n",
    "    'num_threads':24\n",
    "}\n",
    "lgb_train = lgb.Dataset(x_train, y_train)\n",
    "lgb_model = lgb.train(parameters,lgb_train,num_boost_round=200,verbose_eval=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "joblib.dump(lgb_model,'./model/lgb_model')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# predict\n",
    "valid_id = pd.read_csv('../../preprocess_data_new/valid_date.csv',usecols=['id']).values.ravel()\n",
    "model = joblib.load('./model/lgb_model')\n",
    "def predict_prob(model,x_test,test_id):\n",
    "    pred = pd.DataFrame()\n",
    "    pred['id'] = test_id\n",
    "    pred['prob'] = model.predict(x_test)\n",
    "    return pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train.shape,x_valid.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pred = predict_prob(model,x_valid,valid_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pred.to_csv('./preds/lgb_pred.txt',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# AUC：0.819"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
